package cn.dakaqi.volunteer.tag;

import cn.dakaqi.entities.PlatformGroup;
import cn.dakaqi.entities.PlatformGroupVolunteer;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/6/5.
 */
@Service(value = "platformGroupVolunteerService")
@Transactional
public class PlatformGroupVolunteerServiceImpl implements PlatformGroupVolunteerService
{
    @Autowired
    PlatformGroupVolunteerDao platformGroupVolunteerDao;
    @Autowired
    PlatformGroupDao platformGroupDao;

    @Override
    public void save(long groupId, long volunteerId) throws ServiceRuntimeException
    {
        //查询当前社团挡土挂靠了哪些平台
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("groupId", new SearchFilter("groupId", SearchFilter.Operator.EQ, groupId));
        Specification<PlatformGroup> spec = DynamicSpecifications.bySearchFilter(filters.values(), PlatformGroup.class);
        List<PlatformGroup> list =  this.platformGroupDao.findAll(spec);
        //将当前用户加入当前社团所挂靠的所有平台
        if(null != list && list.size()>0)
        {
            for(PlatformGroup pg:list)
            {
                PlatformGroupVolunteer platformGroupVolunteer = new PlatformGroupVolunteer();
                platformGroupVolunteer.setGroupId(pg.getGroupId());
                platformGroupVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
                platformGroupVolunteer.setPlatformId(pg.getPlatformId());
                platformGroupVolunteer.setVolunteerId(volunteerId);
                platformGroupVolunteer.setStatus(DKQConstant.DEL_NO);
                platformGroupVolunteerDao.save(platformGroupVolunteer);
            }
        }

    }

    @Override
    public void save(PlatformGroupVolunteer platformGroupVolunteer) throws ServiceRuntimeException
    {
        platformGroupVolunteerDao.save(platformGroupVolunteer);
    }

    @Override
    public void update(PlatformGroupVolunteer platformGroupVolunteer) throws ServiceRuntimeException
    {
        platformGroupVolunteerDao.save(platformGroupVolunteer);
    }

    @Override
    public void delGroupAndPlat(long groupId, long platformId) throws ServiceRuntimeException
    {
        //删除社团成员的挂靠关系
        platformGroupVolunteerDao.delGroupAndPlat(groupId,platformId);
    }

    @Override
    public void exitGroup(long groupId) throws ServiceRuntimeException
    {
        //删除社团成员的挂靠关系
        platformGroupVolunteerDao.exitGroup(groupId);
    }

    @Override
    public void expelGroup(long groupId) throws ServiceRuntimeException
    {
        //删除社团成员的挂靠关系
        platformGroupVolunteerDao.expelGroup(groupId);
    }
}
